package com.hk.yajin.function.members.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.developmentlib.utills.DeviceUtils;
import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.developmentlib.utills.StringHelper;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.function.MainActivity;
import com.hk.yajin.manager.UserInfoManager;
import com.hk.yajin.model.UserInfo;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterUserNameActivity extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_nick_name)
    EditText etNickName;
    @BindView(R.id.tv_submit_data)
    TextView tvSubmitData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_name);
        ButterKnife.bind(this);
        intiView();
    }

    private void intiView() {
        initToolBar(R.string.set_nick_name);
    }

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(RegisterUserNameActivity.this).setTitle("提示")//设置对话框标题
                .setMessage("昵称设置尚未完成，确定现在退出吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.starItSelf(getApplicationContext(),MainActivity.class);
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }


    private void submitSmsCode() {
        final String name = etNickName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showShortToast(getString(R.string.please_input_code));
            return;
        }

        if (isNetWorkConnect()) {
            showLoadingView();
            Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
            reqParam.put("user_name", name);
            HttpRequestUtils.post(false, UrlConstants.FUNCTION_GET_CHANGE_NICK_NAME, reqParam, false, new HttpCallBack() {
                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    showShortToast("设置成功");
                    UserInfoManager.getInstance().getUserInfo().setUser_name(name);
                    MainActivity.starItSelf(RegisterUserNameActivity.this,MainActivity.class);
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    showShortToast(bean.getMsg());
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    showShortToast(tip);
                }

            });
        }


    }

    @OnClick(R.id.tv_submit_data)
    public void onViewClicked() {
        submitSmsCode();
    }


}
